<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrderDetailsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_details', function (Blueprint $table) {
            $table->increments('id');
            $table->string('order_num')->comment('所属订单编号');
            $table->unsignedInteger('goods_id')->comment('商品编号');
            $table->unsignedInteger('product_id')->comment('货品编号');
            $table->unsignedInteger('num')->comment('数量');
            $table->timestamps();
            //laravel中可用的索引类型index(字段名)是创建一个普通索引,unique(字段名)是创建一个唯一索引
            //索引既可以在创建字段的时候创建,也可以在定义完字段之后创建索引
            //给product_id创建唯一索引,goods_id是可以重复的,product_id单独也不是唯一的,在这里需要创建一个复合唯一索引,order_num和product_id组成的值是唯一的.因为不同订单编号中可以有相同的product_id,但在同一个订单编号中不能有多条同样product_id的记录
//            $table->unique('goods_id');
            $table->unique(['order_num','product_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_details');
    }
}
